//
//  LoadingView.m
//  readyBook
//
//  Created by huang on 12-6-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoadingView.h"
#import <QuartzCore/QuartzCore.h>
#import "WaitView.h"
#import "Global.h"

@implementation LoadingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        backButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];
        [backButton addTarget:self action:@selector(donothing)forControlEvents:UIControlEventTouchUpInside];
        backButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self addSubview:backButton];
        
        UIView *bgdView = [[UIView alloc] initWithFrame:CGRectMake((frame.size.width-40)/2, frame.size.height/2-20, 40, 40)];
        [bgdView setCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];
        bgdView.backgroundColor = [UIColor colorWithWhite:0.1f alpha:.7];
        bgdView.layer.cornerRadius = 7.0f;
        [backButton addSubview:bgdView];
        
        acticityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((frame.size.width-40)/2+12.5, frame.size.height/2-20+12.5, 15, 15)];
        acticityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        acticityIndicator.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        acticityIndicator.hidesWhenStopped = YES;
        [backButton addSubview:acticityIndicator];
        [acticityIndicator  startAnimating];//开
        
        [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(closeLoading) userInfo:nil repeats:NO];
        
        
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame setString:(NSString*)string
{
    self = [super initWithFrame:frame];
    if (self)
    {
        backButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];
        [backButton addTarget:self action:@selector(donothing)forControlEvents:UIControlEventTouchUpInside];
        backButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self addSubview:backButton];
        

        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:labelSize]};
        CGSize totalTextSize = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 30) options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;

        UIView *bgdView = [[UIView alloc] initWithFrame:CGRectMake((frame.size.width-totalTextSize.width-40)/2, frame.size.height/2-30, totalTextSize.width+40, 60)];
        [bgdView setCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];
        bgdView.backgroundColor = [UIColor colorWithWhite:0.1f alpha:.7];
        bgdView.layer.cornerRadius = 7.0f;
        [backButton addSubview:bgdView];

        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake((frame.size.width-totalTextSize.width)/2-20, frame.size.height/2-30, totalTextSize.width+40, 60)];
        label1.text = [NSString stringWithFormat:@"%@",string];
        [label1 setFont:[UIFont fontWithName:@"Arial" size:labelSize]];
        label1.backgroundColor = [UIColor clearColor];
        label1.textColor = [UIColor whiteColor];
        label1.textAlignment = NSTextAlignmentRight;
        [backButton addSubview:label1];
        
        acticityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((frame.size.width-totalTextSize.width-40)/2+12.5, frame.size.height/2-30+22.5, 15, 15)];
        acticityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        acticityIndicator.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        acticityIndicator.hidesWhenStopped = YES;
        [backButton addSubview:acticityIndicator];
        [acticityIndicator  startAnimating];//开
        
        [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(closeLoading) userInfo:nil repeats:NO];
    }
    return self;
}

-(void)donothing
{
    
}

-(void)closeLoading
{
    [acticityIndicator stopAnimating];//关
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
    if (backButton != nil)
    {
        [backButton removeFromSuperview];
        backButton = nil;
    }
    if (acticityIndicator != nil)
    {
        [acticityIndicator removeFromSuperview];
        acticityIndicator = nil;
    }
    [self removeFromSuperview];
}

+(void)closeLoading1
{
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(closeLoading) userInfo:nil repeats:NO];
}

+(void)alerViewMessage:(NSString*)message
{
    //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    //    [alert show];
    
    WaitView *waitView2 = (WaitView *)[[UIApplication sharedApplication].keyWindow viewWithTag:212];
    if (waitView2 == nil)
    {
        waitView2 = [[WaitView alloc] initWithCenterText:message];
        waitView2.tag = 212;
        [[UIApplication sharedApplication].keyWindow addSubview:waitView2];
        waitView2.hidden = YES;
        waitView2 = (WaitView *)[[UIApplication sharedApplication].keyWindow viewWithTag:212];
    }
    else {
        waitView2.label.text = message;
    }
    [waitView2 showWithTimeInInterval:1.0];
    
}


@end
